window.onload = function() {
    var nav = document.getElementById('nav');
    nav.onchange = function() {
    	getBrand(this.value);
    	getAttr(this.value);
    }
}

function checkAddGoods() { 
	var fm = document.add;
	if(fm.nav.value == -1) {
		alert('请务必选择一个商品类型!');
		fm.nav.focus();
		return false;
	}
	if(fm.brand.value == -1) {
		alert('请务必选择一个商品品牌!');
		fm.brand.focus();
		return false;
	}
	if(fm.name.value == '') {
		alert('商品名称不能为空!');
		fm.name.focus();
		return false;
	}
	if(fm.name.value.length < 2) {
		alert('商品名称不能少于两位!');
		fm.name.focus();
		return false;
	}
	if(fm.name.value.length > 100) {
		alert('商品名称不能大于一百位!');
		fm.name.focus();
		return false;
	}
	if(fm.sn.value.length < 2) {
		alert('商品编号不能少于两个字符!');
		fm.sn.focus();
		return false;
	}
	if(fm.sn.value.length > 50) {
		alert('商品编号不能大于五十个字符!');
		fm.sn.focus();
		return false;
	}
	return true;
}

function getBrand(id) {
    var brand = document.getElementById('brand');
    ajax({
    	'method':'get',
    	'async':false,
    	'url':'?a=goods&m=getBrand&id='+id,
    	success:function(text) {
    	   var a = text.split(':');
    	   brand.options.length = 1;       //初始化商品品牌选项，以免累加
    	   for(var i=0;i<a.length;i=i+2) {
    	       brand.options.add(new Option(a[i+1],a[i]));    //按照options的构造函数的格式 value | text
    	   }
    	}
    });
}

function getAttr(id) {
	ajax({
        'method':'get',
        'async':false,
        'url':'?a=goods&m=getAttr&id='+id,
        success:function(text) {
            if(text !== '') {
	            var attrArr = text.split(';');
	            var alldd = document.getElementsByTagName('dd');
	            //从后面开始删除才能删除完整
	            for(var i=alldd.length-1;i>=0;i--) {
	            	if(alldd[i].className == 'temp') {
	            		alldd[i].parentNode.removeChild(alldd[i]);
	            	}
	            }
	            var ddRe = document.getElementsByTagName('dd')[4];
	            for(var i=0;i<attrArr.length;i++) {
	            	var attrName = attrArr[i].substr(0,attrArr[i].indexOf(':'));
	            	var attrPrototype = attrName;
	            	if(attrName.length == 2) {
	            		attrName = attrName.charAt(0) + '　　' + attrName.charAt(1)+'：';
	            	} else if(attrName.length == 3) {
	            		attrName = attrName.charAt(0) + ' ' + attrName.charAt(1) + ' ' + attrName.charAt(2)+'：';
	            	}
	            	
	            	var attrValueArr = attrArr[i].substr(attrArr[i].indexOf(':')+1).split('|');                   
	            	var dd = document.createElement('dd');
	            	var name = document.createTextNode(attrName);
	            	dd.className = 'temp';
	            	dd.appendChild(name);
	            	 for(var j=0;j<attrValueArr.length;j++) {                   
                        var input = document.createElement('input');
                        var text = document.createTextNode(attrValueArr[j]+' ');
                        input.appendChild(text);
                        input.type = 'checkbox';
	                    input.checked = true;
	                    input.name = 'attr['+attrPrototype+'][]';
	                    input.value = attrValueArr[j];
	                    dd.appendChild(input);
	                    dd.appendChild(text);
                    }
	            	ddRe.parentNode.insertBefore(dd,ddRe);
	            } 
	        }
        }
    });
}

function centerWindow(url,name,width,height) {
    var top = (screen.height - height) / 2 - 50;
    var left = (screen.width - width) / 2;
    window.open(url,name,'width='+width+',height='+height+',top='+top+',left='+left);
}